package io.renren.modules.usermanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.R;
import io.renren.common.utils.RoleCode;
import io.renren.modules.companys.dao.CompanyDao;
import io.renren.modules.companys.entity.CompanyEntity;
import io.renren.modules.resumes.dao.ResumeDao;
import io.renren.modules.resumes.entity.ResumeEntity;
import io.renren.modules.sys.dao.SysRoleDao;
import io.renren.modules.sys.dao.SysUserDao;
import io.renren.modules.sys.dao.SysUserRoleDao;
import io.renren.modules.sys.dao.UserBaseInfoDao;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.entity.SysUserRoleEntity;
import io.renren.modules.sys.entity.UserBaseInfoEntity;
import io.renren.modules.sys.service.SysUserService;
import io.renren.modules.usermanager.entity.UserManagerVo;
import io.renren.modules.usermanager.entity.dto.QueryUserCondition;
import io.renren.modules.usermanager.service.UsermanagerService;
import io.renren.modules.workjobs.dao.WorkJobDao;
import io.renren.modules.workjobs.entity.WorkJobEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UsermanagerServiceImpl implements UsermanagerService {

    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private SysUserRoleDao sysUserRoleDao;
    @Autowired
    private SysRoleDao sysRoleDao;
    @Autowired
    private UserBaseInfoDao userBaseInfoDao;
    @Autowired
    private ResumeDao resumeDao;
    @Autowired
    private WorkJobDao workJobDao;
    @Autowired
    private CompanyDao companyDao;


    // TODO
    //@Override
    /*public R getUserList(Long userId,String current,String size) {

        long currentPage = Long.parseLong(current);
        long pageSize = Long.parseLong(size);



        List<UserManagerVo> userManagerVoListData = new ArrayList<>();

        if (RoleCode.SUPER_ADMIN.equals(userId) || RoleCode.SUB_ADMIN.equals(userId)){
            QueryWrapper<SysUserRoleEntity> wrapper = new QueryWrapper<>();
            wrapper.in("role_id",RoleCode.PERSONAL_ROLE,RoleCode.COMPANY_ROLE,RoleCode.HUNTING_ROLE);
            *//*筛选出来的是角色对象*//*
            List<SysUserRoleEntity> sysUserRoleEntities = sysUserRoleDao.selectList(wrapper);
            *//*所有的用户Id*//*
            List<Long> userIds = sysUserRoleEntities.stream().map(item -> item.getUserId()).collect(Collectors.toList());

            QueryWrapper<SysUserEntity> sysUserEntityQueryWrapper = new QueryWrapper<>();
            sysUserEntityQueryWrapper.in("user_id",userIds);
            List<SysUserEntity> sysUserEntities = sysUserDao.selectList(sysUserEntityQueryWrapper);

            QueryWrapper<UserBaseInfoEntity> userBaseInfoEntityQueryWrapper = new QueryWrapper<>();
            userBaseInfoEntityQueryWrapper.in("user_info_id",userIds);
            List<UserBaseInfoEntity> userBaseInfoEntities = userBaseInfoDao.selectList(userBaseInfoEntityQueryWrapper);

            for (UserBaseInfoEntity item : userBaseInfoEntities){
                UserManagerVo userManagerVo = new UserManagerVo();
                userManagerVo.setName(item.getName());
                userManagerVo.setAge(item.getAge());
                userManagerVo.setGender(item.getGender());
                userManagerVo.setUserInfoId(item.getUserInfoId());
                userManagerVo.setPhone(item.getPhone());
                userManagerVo.setEmail(item.getEmail());
                userManagerVo.setQq(item.getQq());
                userManagerVo.setEducationId(item.getEducationId());
                userManagerVo.setAdvator(item.getAdvator());
                userManagerVo.setCreateTime(item.getCreateTime());
                userManagerVo.setWechat(item.getWechat());
                userManagerVo.setId(item.getId());
                userManagerVoListData.add(userManagerVo);
            }

        }
        return R.ok().put("data",userManagerVoListData);
    }*/

    @Override
    public R removeUserInfo(List<Long> asList) {
        /*删除用的职位信息*/
        QueryWrapper<WorkJobEntity> workJobEntityQueryWrapper = new QueryWrapper<>();
        workJobEntityQueryWrapper.in("create_user_id",asList);
        List<WorkJobEntity> workJobEntities = workJobDao.selectList(workJobEntityQueryWrapper);
        List<String> workJobIds = workJobEntities.stream().map(item -> item.getId()).collect(Collectors.toList());
        if (workJobIds.size()>0){
            workJobDao.deleteBatchIds(workJobIds);
        }

        /*删除企业信息*/
        QueryWrapper<CompanyEntity> companyEntityQueryWrapper = new QueryWrapper<>();
        companyEntityQueryWrapper.in("create_user_id",asList);
        List<CompanyEntity> companyEntities = companyDao.selectList(companyEntityQueryWrapper);
        List<String> companyIds = companyEntities.stream().map(item -> item.getId()).collect(Collectors.toList());
        if (companyIds.size()>0){
            companyDao.deleteBatchIds(companyIds);
        }

        /*删除用户的简历信息*/
        QueryWrapper<ResumeEntity> resumeEntityQueryWrapper = new QueryWrapper<>();
        resumeEntityQueryWrapper.in("user_id",asList);
        List<ResumeEntity> resumeEntities = resumeDao.selectList(resumeEntityQueryWrapper);
        List<Long> resumeIds = resumeEntities.stream().map(item -> item.getId()).collect(Collectors.toList());
        if (resumeIds.size()>0){
            resumeDao.deleteBatchIds(resumeIds);
        }

        /*删除用户的所属角色*/
        QueryWrapper<SysUserRoleEntity> userRoleEntityQueryWrapper = new QueryWrapper<>();
        userRoleEntityQueryWrapper.in("user_id",asList);
        List<SysUserRoleEntity> sysUserRoleEntities = sysUserRoleDao.selectList(userRoleEntityQueryWrapper);
        List<Long> userRoleIds = sysUserRoleEntities.stream().map(item -> item.getId()).collect(Collectors.toList());
        if (userRoleIds.size()>0){
            sysUserRoleDao.deleteBatchIds(userRoleIds);
        }
        /*删除用户的基本信息*/
        QueryWrapper<UserBaseInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.in("user_info_id",asList);
        List<UserBaseInfoEntity> userBaseInfoEntities = userBaseInfoDao.selectList(wrapper);
        List<Long> userBaseInfoIds = userBaseInfoEntities.stream().map(item -> item.getId()).collect(Collectors.toList());
        if (userBaseInfoIds.size()>0){
            userBaseInfoDao.deleteBatchIds(userBaseInfoIds);
        }

        return R.ok();
    }

    @Override
    public R getUserList(long userId, String current, String size, QueryUserCondition queryUserCondition) {

        long currentPage = Long.parseLong(current);
        long pageSize = Long.parseLong(size);

        IPage page = new Page();
        page.setCurrent(currentPage);
        page.setSize(pageSize);

        List<UserManagerVo> userManagerVoListData = new ArrayList<>();

        QueryWrapper<SysUserRoleEntity> wrapper = new QueryWrapper<>();
        wrapper.in("role_id",RoleCode.PERSONAL_ROLE,RoleCode.COMPANY_ROLE);
        List<SysUserRoleEntity> sysUserRoleEntities = sysUserRoleDao.selectList(wrapper);
        //所有的用户Id
        List<Long> userIds = sysUserRoleEntities.stream().map(item -> item.getUserId()).collect(Collectors.toList());
        QueryWrapper<SysUserEntity> sysUserEntityQueryWrapper = new QueryWrapper<>();
        sysUserEntityQueryWrapper.in("user_id",userIds);

        sysUserDao.selectPage(page,sysUserEntityQueryWrapper);

        List records = page.getRecords();

        QueryWrapper<UserBaseInfoEntity> userBaseInfoEntityQueryWrapper = new QueryWrapper<>();
        userBaseInfoEntityQueryWrapper.in("user_info_id",userIds);

        if (queryUserCondition.getName()!=null){
            userBaseInfoEntityQueryWrapper.like("name",queryUserCondition.getName());
        }

        List<UserBaseInfoEntity> userBaseInfoEntities = userBaseInfoDao.selectList(userBaseInfoEntityQueryWrapper);

        for (UserBaseInfoEntity item : userBaseInfoEntities){
            UserManagerVo userManagerVo = new UserManagerVo();
            userManagerVo.setName(item.getName());
            userManagerVo.setAge(item.getAge());
            userManagerVo.setGender(item.getGender());
            userManagerVo.setUserInfoId(item.getUserInfoId());
            userManagerVo.setPhone(item.getPhone());
            userManagerVo.setEmail(item.getEmail());
            userManagerVo.setQq(item.getQq());
            userManagerVo.setEducationId(item.getEducationId());
            userManagerVo.setAdvator(item.getAdvator());
            userManagerVo.setCreateTime(item.getCreateTime());
            userManagerVo.setWechat(item.getWechat());
            userManagerVo.setId(item.getId());
            userManagerVoListData.add(userManagerVo);
        }
        return R.ok().put("data",userManagerVoListData).
                put("total",userBaseInfoEntities.size());
    }
}
